#include "GameoverState.h"

GameoverState* GameoverState::gameoverState = 0;


GameoverState::GameoverState(sf::RenderWindow* window, sfge::StateManager* mgr) : sfge::State(window, mgr)
{

    //nameEdit = new TextEdit("THIS IS A test", 200, 200, 200);
}

GameoverState::~GameoverState()
{

}

void GameoverState::init()
{
    if(tBestScore < points)
        tBestScore = points;
    cursorSprite.SetTexture(rm->getTexture("cursor.png"));
    gameoverText.SetString("Game Over Bitch!");
    gameoverText.SetCharacterSize(25);
    setCenter(gameoverText, 200);
    scoreText.SetString("Your score: " + to_string(points));
    scoreText.SetCharacterSize(23);
    setCenter(scoreText, 250);
    playAgainButton = new Button("Play again", 0, 360);
    quitButton = new Button("Quit", 0, 440);
}

void GameoverState::cleanup()
{
    delete playAgainButton;
    delete quitButton;
}

void GameoverState::checkButtons(float x, float y)
{
    playAgainButton->checkHL(x, y);
    quitButton->checkHL(x, y);
}

void GameoverState::clicked(float x, float y)
{
    //nameEdit->checkClick(x, y);
    if(playAgainButton->isClicked(x, y))
    {
        stateManager->popState();
        return;
    }
    if(quitButton->isClicked(x, y))
    {
        stateManager->popState();
        stateManager->popState(); //pop this state//pop GameState also
        return;
    }
}

void GameoverState::handle()
{
    sf::Event Event;
    while (window->PollEvent(Event))
    {
        switch (Event.Type)
        {
            case sf::Event::Closed:
                window->Close();
                break;
            case sf::Event::MouseMoved:
                    checkButtons(window->ConvertCoords(sf::Mouse::GetPosition(*window).x, 0).x, window->ConvertCoords(0, sf::Mouse::GetPosition(*window).y).y);
                    cursorSprite.SetPosition(window->ConvertCoords(sf::Mouse::GetPosition(*window).x, 0).x, window->ConvertCoords(0, sf::Mouse::GetPosition(*window).y).y);
                break;
            case sf::Event::MouseButtonPressed:
                if(Event.MouseButton.Button == sf::Mouse::Left)
                {
                    clicked(window->ConvertCoords(sf::Mouse::GetPosition(*window).x, 0).x, window->ConvertCoords(0, sf::Mouse::GetPosition(*window).y).y);
                    return;
                }
                break;
            default: break;
        }
    }
}

void GameoverState::draw()
{
    window->Clear(sf::Color(0, 0, 0));
    window->Draw(gameoverText);
    window->Draw(scoreText);
    quitButton->draw(window);
    playAgainButton->draw(window);
    window->Draw(cursorSprite);
    //nameEdit->draw(window);
    window->Display();
}

void GameoverState::update()
{

}


